﻿using Iworld.Author.Model;
using Iworld.basis.Service;
using Iworld.Basis.Help;
using Iworld.Basis.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
 
namespace Iworld.Author.Service.Client.Import
{
    /// <summary>
    /// 创建提现记录
    /// </summary>
    [DataContract]
    public class CreateWithdrawalsRecordImport : CheckAuthorImportBase, IPackageForCreateModel<IModelToDbContextOfAuthor, Iworld.Author.Model.WithdrawalsRecord>
    {
        /// <summary>
        /// 提现金币数量
        /// </summary>
        [DataMember]
        public double Golds { set; get; }

        /// 账号
        /// </summary>
        [DataMember]
        public string Account { get; set; }

        /// <summary>
        /// 银行
        /// </summary>
        [DataMember]
        public string Bank { set; get; }

        /// <summary>
        /// 开户人
        /// </summary>
        [DataMember]
        public string Person { set; get; }

        /// <summary>
        /// 二级密码
        /// </summary>
        [DataMember]
        public string TwoLevelPassword { get; set; }

        public void CheckData(IModelToDbContextOfAuthor db)
        {
            if (this.TwoLevelPassword == null || this.Self.TwoLevelPassword != EncryptHelper.EncryptByMd5(this.TwoLevelPassword))
            {
                throw new Exception("二级密码不正确,提现失败");
            }

            if(string.IsNullOrWhiteSpace(Account) || string.IsNullOrWhiteSpace(Bank) || string.IsNullOrWhiteSpace(Person))
            {
                throw new Exception("请输入完整的信息,提现失败");
            }

            if(Golds <=0)
            {
                throw new Exception("金币不能小于等于0,提现失败");
            }
            Iworld.Author.Model.Author user = db.Authors.FirstOrDefault(x => x.Id == this.Self.Id);
            if (user == null) throw new Exception("提醒下失败,当前用户不存在");
            if (user.Golds < Golds)
            {
                throw new Exception("余额不足,提现失败");
            }
        }

        public Model.WithdrawalsRecord GetModel(IModelToDbContextOfAuthor db)
        {
            WithdrawalsRecord re = new WithdrawalsRecord();
            re.Owner = db.Authors.Find(this.Self.Id);
            re.Account = this.Account;
            re.Bank = this.Bank;
            re.Person = this.Person;
            re.Golds = this.Golds;
            return re;
        }
    }
}
